package com.example.studentknowledgesys.dao;

import com.example.studentknowledgesys.entity.PointRecord;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface PointRecordMapper {
    // 插入积分记录
    @Insert("INSERT INTO point_records(user_id, rule_id, points, description) " +
            "VALUES(#{userId}, #{ruleId}, #{points}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertPointRecord(PointRecord pointRecord);

    // 查询用户积分记录
    @Select("SELECT pr.*, r.rule_name, r.rule_type " +
            "FROM point_records pr " +
            "JOIN point_rules r ON pr.rule_id = r.id " +
            "WHERE pr.user_id = #{userId} " +
            "ORDER BY pr.action_time DESC")
    List<PointRecord> selectPointRecordsByUserId(Long userId);

    // 查询所有积分记录
    @Select("SELECT pr.*, r.rule_name, r.rule_type, u.username " +
            "FROM point_records pr " +
            "JOIN point_rules r ON pr.rule_id = r.id " +
            "JOIN users u ON pr.user_id = u.id " +
            "ORDER BY pr.action_time DESC")
    List<PointRecord> selectAllPointRecords();
}
